package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
// 跨域
@CrossOrigin
public class ArticleController {

  @Autowired ArticleService articleService;

  @Autowired RedisTemplate<String, String> redisTemplate;

  /**
   * 文章点赞
   * @param articleId
   * @return
   */
  @RequestMapping(value = "/thumbup/{articleId}", method = RequestMethod.PUT)
  public Result thumbup(@PathVariable String articleId) {
    // TODO jwt获取用户ID
    String userId = "1";
    String key = "thumbup_article_" + userId + "_" + articleId;

    String flag = redisTemplate.opsForValue().get(key);

    if (flag == null) {
      articleService.thumbup(userId, articleId);
      redisTemplate.opsForValue().set(key, "1");
      return new Result(true, StatusCode.OK, "点赞成功");
    }

    return new Result(false, StatusCode.REPERROR, "不能重复点赞");
  }

  /**
   * 订阅或取消订阅文章作者
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/subscribe", method = RequestMethod.POST)
  public Result subscribe(@RequestBody Map map) {
    Boolean flag =
        articleService.subscribe(map.get("userId").toString(), map.get("articleId").toString());
    if (flag) {
      return new Result(true, StatusCode.OK, "订阅成功");
    } else {
      return new Result(true, StatusCode.OK, "订阅取消");
    }
  }

  @RequestMapping(method = RequestMethod.GET)
  public Result findAll() {
    List list = articleService.findAll();
    return new Result(true, StatusCode.OK, "查询成功", list);
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public Result findById(@PathVariable String id) {
    Article article = articleService.findById(id);
    return new Result(true, StatusCode.OK, "查询成功", article);
  }

  /**
   * 增加文章
   *
   * @param article 文章类实体对象
   * @return
   */
  @RequestMapping(method = RequestMethod.POST)
  public Result add(@RequestBody Article article) {
    articleService.add(article);
    return new Result(true, StatusCode.OK, "添加成功");
  }

  /**
   * 根据ID修改文章
   *
   * @param id 文章id
   * @param article 文章类实体对象
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  public Result update(@PathVariable String id, @RequestBody Article article) {
    article.setId(id);
    articleService.update(article);
    return new Result(true, StatusCode.OK, "修改成功");
  }

  /**
   * 根据ID删除文章
   *
   * @param id
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public Result delete(@PathVariable String id) {
    articleService.delete(id);
    return new Result(true, StatusCode.OK, "删除成功");
  }

  /**
   * 条件查询并分页
   *
   * @param map 查询条件
   * @param page 当前页
   * @param size 页大小
   * @return
   */
  @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
  public Result search(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
    Page page1 = articleService.search(map, page, size);
    return new Result(
        true, StatusCode.OK, "查询成功", new PageResult((int) page1.getTotal(), page1.getRecords()));
  }

  /** 统一异常处理测试 */
  @RequestMapping(value = "/exception", method = RequestMethod.GET)
  public Result exception() throws Exception {
    throw new Exception("测试统一异常处理");
  }
}
